<?php
class Front_LoginController extends BasePage implements IController {
	/**
	 * 构造函数。
	 *
	 */
	function __construct() {
		parent::__construct();
	}
	
	/**
	 * 控制转发器。
	 *
	 */
	function dispatcher() {
		if ($this->isPostback) {
			if ($this->action == 'login') {
				$this->login();
			}
		} else {
			if ($this->isLogined) {
				if ($this->action == 'logout') {
					$this->logout();
				}
				$this->go('index.php');
			}
			$this->display('login');
		}
	}
	
	/**
	 * 处理登录。
	 *
	 */
	function login() {
		$d = $this->db->fetch("SELECT * FROM `tbg_users` WHERE `username` = ? AND `password` = ?", array(
			$this->forms['username'], 
			sha1($this->forms['userpass'])
		));
		if ($d) {
			$_SESSION['TBG_U_SESS'] = array(
				$d['user_id'], 
				$d['username'], 
				$d['nickname'], 
				$d['is_admin']
			);
			
			$this->go('index.php');
		} else {
			$this->go();
		}
	}
	
	/**
	 * 注销登录。
	 *
	 */
	function logout() {
		unset($_SESSION['TBG_U_SESS']);
		
		$this->go('index.php');
	}
}